package com.src.myapplication.widget;

import android.content.Intent;
import android.net.Uri;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.src.myapplication.R;
import com.src.myapplication.base.BaseActivity;
import com.src.myapplication.ui.moudle.Mode;

import butterknife.BindView;
import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.widget.CenterLayout;
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;

/**
 * Created by Administrator on 2016/10/20.
 */
public class PlayActivity extends BaseActivity  {
    @BindView(R.id.surface_view)
    VideoView surfaceView;
    @BindView(R.id.iv)
    ImageView iv;
    @BindView(R.id.Cl)
    CenterLayout Cl;
    @BindView(R.id.bt)
    CheckBox bt;
    private MediaController mediaController;
    private Mode mMode;
    @Override
    protected void initView() {
        mMode=new Mode();
        Intent intent = getIntent();
        final String[] keys = intent.getStringArrayExtra("key");
        final String id =keys[0];
        String name =keys[1];
        String imag =keys[2];
        String url =keys[3];
        Glide.with(PlayActivity.this).load(imag).into(iv);
        paly(url);
        bt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
//                    boolean query = mMode.query(PlayActivity.this, keys[0]);
//                    if(query==false){
                        mMode.save(PlayActivity.this,keys);
//                    }


                }else {
//                    mMode.delete(PlayActivity.this,)
                    Toast.makeText(PlayActivity.this,"取消收藏",Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_paly;
    }

    private void paly(String url) {//播放视频

        if (url == null) {//判断视频地址不为空
            Toast.makeText(PlayActivity.this, "播放地址无效", Toast.LENGTH_LONG).show();
        } else {
            surfaceView.setVideoURI(Uri.parse(url));//传入一个URL
            surfaceView.setMediaController(mediaController);//把VideoView对象与控制器绑定
            surfaceView.requestFocus();//VideoView获得焦点
            surfaceView.setVideoQuality(View.DRAWING_CACHE_QUALITY_HIGH);
            surfaceView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                public boolean onInfo(MediaPlayer mp, int what, int extra) {
                    //缓冲开始
                    if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {
                        iv.setVisibility(View.VISIBLE);
                        //缓冲结束
                    } else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
                        //此接口每次回调完START就回调END,若不加上判断就会出现缓冲图标一闪一闪的卡顿现象
                        iv.setVisibility(View.GONE);
                        bt.setVisibility(View.VISIBLE);
                        //   mp.start();
                        surfaceView.start();
                    }
                    return true;
                }
            });
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            surfaceView.stopPlayback();
            finish();
        }
        return false;
    }


}